<template>
  <div class="brand-class-list table-theme">
    <head-title title="运营--品牌分类列表"></head-title>
    <el-card>
      <div class="top-menu">
        <div class="menu-left"></div>
        <div class="menu-right">
          <refresh-button @refresh="refreshPage"></refresh-button>
          <search-view
            :option="optionList"
            @value="goSearch"
            @clear="refreshPage"
          ></search-view>
          <div class="add">
            <el-button type="primary" @click="onOpenDialog('add')"
              >添加分类</el-button
            >
          </div>
        </div>
      </div>
      <p class="data-number">总列表数：{{ total }}</p>
      <el-table :data="list" stripe>
        <el-table-column
          label="序号"
          align="center"
          prop="class_id"
        ></el-table-column>
        <el-table-column
          label="分类名称"
          align="center"
          prop="class_name"
        ></el-table-column>
        <el-table-column label="排序" align="center">
          <template slot-scope="scope">
            <el-input
              v-model.number="scope.row.sort"
              @pause="scope.row.sort = scope.row.sort.replace(/[^\d]/g, '')"
              @keyup.enter.native="onUpSort(scope.row.sort, scope.row.class_id)"
              @blur="onUpSort(scope.row.sort, scope.row.class_id)"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column
          label="时间"
          align="center"
          prop="add_time"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="onOpenDialog('edit', scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="primary"
              @click="onOpenDialog('details', scope.row)"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="onDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pages">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="init"
          :current-page="page"
          :page-size="limit"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="title"
      :visible.sync="isDialog"
      center
      :modal-append-to-body="false"
      width="500px"
      @close="onClose('classForm')"
    >
      <el-form
        :model="classForm"
        :rules="classFormRules"
        ref="classForm"
        label-width="120px"
      >
        <el-form-item label="分类名称:" prop="class_name">
          <el-input
            v-model="classForm.class_name"
            placeholder="请输入分类名称"
            :disabled="type === 'details'"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序:" prop="sort">
          <el-input
            v-model="classForm.sort"
            placeholder="请输入排序"
            :disabled="type === 'details'"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose('classForm')">取 消</el-button>
        <el-button
          type="primary"
          @click="onSubimt('classForm')"
          v-if="type !== 'details'"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      classSelect: 0,
      selectContent: "",
      store_id: localStorage.getItem("store_id"),
      page: 1,
      limit: 30,
      total: 0,
      list: [],
      optionList: [
        {
          value: 0,
          label: "分类ID",
        },
        {
          value: 1,
          label: "名称",
        },
      ],
      isDialog: false,
      title: "",
      type: "add",
      classForm: {
        class_name: "",
        sort: 0,
      },
      classFormRules: {
        class_name: [{ required: true, message: "请输入分类名称" }],
        sort: [{ required: true, message: "请输入分类排序" }],
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    // 清除搜索内容
    refreshPage() {
      this.page = 1;
      this.classSelect = 0;
      this.selectContent = "";
      this.init(1);
    },
    // 获取搜索框
    goSearch(type, value) {
      this.selectContent = value;
      this.classSelect = type;
      this.init();
    },
    init(e) {
      this.page = e;
      let data = {
        page: e ? e - 1 : 0,
        limit: this.limit,
        store_id: this.store_id,
      };
      if (this.classSelect == 0) {
        data.class_id = this.selectContent;
      } else if (this.classSelect == 1) {
        data.keyword = this.selectContent;
      }
      this.$http(this.$ApiList.StoreFactoryClassList, data).then((res) => {
        this.total = res.datas.count;
        this.list = res.datas.list;
      });
    },
    onUpSort(sort, class_id) {
      if (sort === "" || sort <= -1) {
        return false;
      }
      this.$http(this.$ApiList.UpSortFactoryClass,{sort,class_id}).then(res=>{
          this.init(this.page)
      })
    },
    onOpenDialog(type, data) {
      this.type = type;
      switch (type) {
        case "add":
          this.title = "添加品牌分类";

          break;
        case "edit":
        case "details":
          this.title = type === "edit" ? "编辑品牌分类" : "品牌分类详情";
          this.$http(this.$ApiList.StoreFactoryClassInfo, {
            class_id: data.class_id,
            store_id: data.store_id,
          }).then((res) => {
            delete res.datas.add_time;
            this.classForm = res.datas;
          });
          break;
        default:
          break;
      }
      this.isDialog = true;
    },
    onClose(formName) {
      this.isDialog = false;
      this.$refs[formName].resetFields();
    },
    onSubimt(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = this.classForm;
          data.store_id = this.store_id;
          if (this.type === "add") {
            delete data.class_id;
            this.$http(this.$ApiList.StoreFactoryClassAdd, data).then((res) => {
              this.$message.success(res.datas);
              this.init(this.page);
              this.isDialog = false;
              this.$refs[formName].resetFields();
            });
          } else if (this.type === "edit") {
            this.$http(this.$ApiList.StoreFactoryClassUpdate, data).then(
              (res) => {
                this.$message.success(res.datas);
              this.init(this.page);
              this.isDialog = false;
              this.$refs[formName].resetFields();
              }
            );
          }
        }
      });
    },
    onDel(data){
         this.$confirm("此操作将永久删除该品牌分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http(this.$ApiList.StoreFactoryClassDelete, {store_id:data.store_id,class_id:data.class_id}).then((res) => {
            this.$message.success(res.datas);
            this.init(this.page);
          });
        })
        .catch(() => {});
    }
  },
};
</script>

<style lang="scss" scoped>
.brand-class-list {
}
</style>